/*  extMDB 1.1 - an extension to gvSIG Desktop GIS to read GeoMedia MDB warehouses
 *  Copyright (C) 2008-2009  Stefano Orlando
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * Revision 1.1  02/05/09 Stefano Orlando
 * Revision 1.0  06/02/09 Stefano Orlando
 */
package com.iver.cit.gvsig.mdb.core;

import java.sql.SQLException;
import com.iver.andami.PluginServices;
/**
 * Exception thrown when an error occurs accessing a GeoMedia Warehouse.
 * @author Stefano Orlando
 */
public class MDBException extends Exception {

	private static final long serialVersionUID = 7085938705088950251L;
	
	private String msg;
	
	public MDBException(String msg) {
		super();
		this.msg = formatMessage(msg, null);
	}
	
	public MDBException(String msg, Throwable cause) {
		super(cause);
		this.msg = formatMessage(msg, null);
	}
	
	public MDBException(String msg, SQLException ex) {
		super();
		this.msg = formatMessage(msg, ex);
	}	

	public MDBException(String msg, SQLException ex, Throwable cause) {
		super(cause);
		this.msg = formatMessage(msg, ex);
	}
	
	public MDBException(Throwable cause) {
		super(cause);
	}
	
	public String getMessage() {
		return this.msg;
	}
	
	private String formatMessage(String msg, SQLException sqlEx) {
		
		if (msg.equalsIgnoreCase("JDBC-ODBC_bridge_not_found_error"))
			return PluginServices.getText(this,"JDBC-ODBC_bridge_not_found_error");
		
		else if (msg.equalsIgnoreCase("HSQLDB_engine_driver_not_found_error"))
			return PluginServices.getText(this,"HSQLDB_engine_driver_not_found_error");		
		
		else if (msg.equalsIgnoreCase("db_conn_error"))
			return PluginServices.getText(this,"db_conn_error");
		
		else if (msg.equalsIgnoreCase("format_error"))
			return PluginServices.getText(this,"format_error");
		
		else if (msg.equalsIgnoreCase("unexpected_SQL_error")) {
			while (sqlEx != null) {
				System.err.println(
					"------------------- SQLException -------------------\n" +
					"Message:   " + sqlEx.getMessage () + "\n" +
					"SQLState:  " + sqlEx.getSQLState () + "\n" +
					"ErrorCode: " + sqlEx.getErrorCode () + "\n" +
					"----------------------------------------------------");
				sqlEx = sqlEx.getNextException();
			}
			return PluginServices.getText(this,"unexpected_SQL_error");
		}
		
		else if (msg.equalsIgnoreCase("OS_not_supported_error")) {
			return PluginServices.getText(this,"OS_not_supported_error");
		}
		
		else return msg;
	}
}
